﻿// <copyright file="LinkValidator.cs" company="City of Portland, BES-ASM">
// </copyright>
// <summary>LinkValidator class</summary>

namespace BESASM.EMGAATS.BusinessClasses
{
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using FluentValidation;
  using FluentValidation.Validators;

  /// <summary>
  /// A class for validating Link instances
  /// </summary>
  public class LinkValidator : AbstractValidator<ILink>
  {
    /// <summary>
    /// Initializes a new instance of the <see cref="LinkValidator"/> class.
    /// </summary>
    /// <param name="ruleSet">The set of rules to apply.</param>
    public LinkValidator(RuleSet ruleSet)
    {
      // Universal rules

      // RuleSet rules
      switch (ruleSet)
      {
        case BusinessClasses.RuleSet.MasterDataCommit:
          this.LinkInvertsBelowGroundLevel(ErrorLevel.Error);
          this.LinkHeightGreaterThanZero(ErrorLevel.Error);
          break;
        case BusinessClasses.RuleSet.ModelDataCommit:
          this.LinkInvertsBelowGroundLevel(ErrorLevel.Error);
          this.LinkHeightGreaterThanZero(ErrorLevel.Error);
          break;
        case BusinessClasses.RuleSet.ModelDeployment:
          this.LinkInvertsBelowGroundLevel(ErrorLevel.Error);
          this.LinkHeightGreaterThanZero(ErrorLevel.Error);
          break;
        case BusinessClasses.RuleSet.NetworkTracing:
          break;
        case BusinessClasses.RuleSet.PropertyDialog:
          this.LinkHeightGreaterThanZero(ErrorLevel.Error);
          this.LinkInvertsBelowGroundLevel(ErrorLevel.Warning);
          break;
      }
    }

    private void LinkHeightGreaterThanZero(ErrorLevel level)
    {
      RuleFor(link => link.HeightIn)
        .GreaterThan(0.0)
        .WithState(link => new ValidationState(link, level))
        .WithMessage("Link ({0}) has a height equal or less than zero.", link => link.LinkName);
    }

    private void LinkInvertsBelowGroundLevel(ErrorLevel level)
    {
      RuleFor(link => link.USIEFt)
        .Must(this.BeBelowGroundLevel)
        .WithState(link => new ValidationState(link, level))
        .WithMessage("Upstream invert elevation for link ({0}) is above ground level.", link => link.LinkName);

      RuleFor(link => link.DSIEFt)
        .Must(this.BeBelowGroundLevel)
        .WithState(link => new ValidationState(link, level))
        .WithMessage("Downstream invert elevation for link ({0}) is above ground level.", link => link.LinkName);
    }

    private bool BeBelowGroundLevel(ILink link, double elevation, PropertyValidatorContext context)
    {
      if (context.PropertyName == "USIEFt")
      {
        if (link.USNode != null)
        {
          return elevation < link.USNode.GroundElevFt;
        }
      }

      if (context.PropertyName == "DSIEFt")
      {
        if (link.DSNode != null)
        {
          return elevation < link.DSNode.GroundElevFt;
        }
      }

      return true;
    }
  }
}
